xquery version "1.0";

module namespace app1 = "http://www.webcomposite.com/app1";

import module namespace xsgi="http://www.webcomposite.com/xsgi";

declare function app1:invoketest($e){
let $env    := if (empty($e)) then xsgi:env() else $e
let $status := <xsgi:status>200</xsgi:status>
let $headers:= <xsgi:headers>
                    <content-type>text/plain</content-type>
                    <echovar>true</echovar>
                </xsgi:headers>
let $response := <html>
					<body>
						<h1>Invoke Test</h1>
						<textarea rows="20" cols="40">{//faq}</textarea>
					</body>
				 </html>
    return
        ($env,$status,$headers,$response)
};

declare function app1:print($e){
let $env    := if (empty($e)) then xsgi:env() else $e
let $status := <xsgi:status>200</xsgi:status>
let $headers:= <xsgi:headers>
                    <content-type>text/plain</content-type>
                    <echovar>true</echovar>
                </xsgi:headers>
let $response:= <html><body><h1>Print out</h1></body></html>
    return
        ($env,$status,$headers,$response)
};

declare function app1:echopost($e){
let $env    := if (empty($e)) then xsgi:env() else $e
let $status := <xsgi:status>200</xsgi:status>
let $headers:= <xsgi:headers>
                    <content-type>text/plain</content-type>
                    <echovar>true</echovar>
                </xsgi:headers>
let $response := $env//xsgi:input/node()
    return
        ($env,$status,$headers, if (empty($response)) then <message>nothing posted</message> else $response )
};

declare function app1:middleware($e){
let $env    := if (empty($e)) then xsgi:env() else $e
let $res    := (app1:print($env))
let $status := $res[2]
let $headers:= <xsgi:headers>
                <X-XSGI-Used>1</X-XSGI-Used>
                {$res[3]/*}
               </xsgi:headers>
    return
        ($env,$status,$headers,$res[4])
};